package org.jeecg.modules.app.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.config.BaiduAipClient;
import org.jeecg.modules.app.service.BaseAppInstallService;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Service
@RequiredArgsConstructor
@Slf4j
public class BaseAppInstallServiceImpl implements BaseAppInstallService {

    private final BaiduAipClient baiduAipClient;

    @Override
    public String baiduCarNoOcr(MultipartFile file) {
        try {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("multi_detect", "false");

            byte[] image = file.getBytes();
            JSONObject jsonObject = baiduAipClient.createAipOcr().plateLicense(image, options);
            if (jsonObject != null) {
                if (jsonObject.has("words_result")) {
                    JSONObject wordsResult = jsonObject.getJSONObject("words_result");
                    if (wordsResult.has("number")) {
                        return wordsResult.getString("number");
                    }
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
